// house_pkg/pages/locate/index.ts
import QQMap from '../../../utils/qqmap'
Page({
  data: {
    address: '',
    points: []
  },
  onLoad() {
    this.getLocation()
  },
  async getLocation() {
    const {
      latitude,
      longitude
    } = await wx.getLocation()
    this.getPos(latitude, longitude)
  },
  // 获取位置，地址逆解析（经纬度 => 地址）
  getPos(latitude, longitude) {
    wx.showLoading({
      title: '加载中...',
    })
    QQMap.reverseGeocoder({
      location: [latitude, longitude].join(),
      success: ({
        result: {
          address
        }
      }) => {
        this.setData({
          address
        })
      }
    })

    QQMap.search({
      keyword: '周边小区',
      location: [latitude, longitude].join(),
      page_size: 5,
      success: (res) => {
        this.setData({
          points: res.data.map(({
            id,
            title,
          }) => {
            return {
              id,
              title,
            }
          })
        })
      },
      fail: (e) => {
        this.setData({
          points: [{
            id: 'fdsfsdfsd',
            title: '五福路小区-一号门楼',
          }]
        })
        console.log(e)
      },
      complete() {
        wx.hideLoading()
      }
    })
  },
  async chooseLocation() {
    const {
      latitude,
      longitude
    } = await wx.chooseLocation()
    this.getPos(latitude, longitude)
  },
  goBuilding(ev) {
    // console.log(ev)
    // ...
    /* wx.navigateTo({
      url: `/house_pkg/pages/building/index?point=${ev.target.dataset.point}`,
    }) */
    wx.navigateTo({
      url: `/house_pkg/pages/building/index?point=${ev.mark.point}`,
    })
  }
})